/*******************************************************************************

This code file produces Figure 1, "Supply Responses to Two Changes in 421-a Incentives" and Figure A8, "Goodness-of-Fit Check Using 421-a Reforms."

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"
	
********************************************************************************
* Create figures
********************************************************************************

*** Load data

	use "$data/clean/cleaned_data.dta", clear
	
	logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta)
	predict pr_421a, pr
	
*** Buildings w/ lower taxable shares were less likely to participate

/*
	tw 	(lpolyci inclusionary_onsite sh_taxable, deg(1) yaxis(1) ///
		xtitle("Taxable Share of Market Value in 2019") xscale(range(0 0.45)) xlabel(0(0.05)0.45) yscale(range(-0.05 0.1))) ///
		(lpoly inclusionary_onsite sh_taxable if sh_taxable > 0.06, deg(1) yaxis(1) lpattern(dash)) ///
		(hist sh_taxable, yaxis(2) ytitle("Share of Buildings", axis(2)) xline(0.06, lcolor(gs9) lpattern(dash)) fraction ylabel(0(0.1)0.2, axis(2)) yscale(range(0 1) axis(2))), ///
		graphregion(color(white)) legend(order(2 "Share with Onsite Inclusionary Units" 4 "Share"))
*/

*** GEA original, expansion, never

	preserve
	
	* Fix data error
	replace gea_expansion = 0 if gea_original == 1
	
	collapse (mean) pr_421a inclusionary_onsite dtaxrate_onsite, by(yearpermit gea_expansion gea_original)
	
	gen gea = 0
	replace gea = 1 if gea_original == 1
	replace gea = 2 if gea_expansion == 1
		
	drop gea_original gea_expansion
	
	reshape wide pr_421a inclusionary_onsite dtaxrate_onsite, i(year) j(gea)
	
	
	tw line dtaxrate_onsite2 dtaxrate_onsite1 dtaxrate_onsite0 yearpermit, ///
		lcolor(black gs8 gs4) lpattern(solid solid dash) lwidth(medthick medthick medthick) ///
		graphregion(color(white)) legend(order(1 "GEA Expansion" 2 "Always GEA" 3 "Never GEA") rows(1) size(small)) ///
		ytitle("Average 421-a Incentive") name(gea_rate, replace) xtitle("") ylabel(,nogrid) xscale(range(2003 2014)) xlabel(2003(3)2015)
		
	tw line inclusionary_onsite2 inclusionary_onsite1 inclusionary_onsite0 yearpermit, ///
		lcolor(black gs8 gs4) lpattern(solid solid dash) lwidth(medthick medthick medthick) ///
		graphregion(color(white)) legend(order(1 "GEA Expansion" 2 "Always GEA" 3 "Never GEA") rows(1) size(small)) ///
		ytitle("Share with Onsite Inclusionary Units") name(gea_sh, replace) xtitle("") ylabel(,nogrid) xscale(range(2003 2014))  xlabel(2003(3)2015)
	
	grc1leg gea_rate gea_sh, graphregion(color(white)) name(gr1, replace)
	
	* Check goodness-of-fit
	
	tw line pr_421a2 pr_421a1 pr_421a0 yearpermit, ///
		lcolor(black gs8 gs4) lpattern(solid solid dash) lwidth(medthick medthick medthick)  ///
		graphregion(color(white)) legend(order(1 "GEA Expansion" 2 "Always GEA" 3 "Never GEA") rows(1) size(small)) ///
		ytitle("") name(gea_sh_pred, replace) xtitle("") ylabel(,nogrid) ///
		xscale(range(2003 2014)) xlabel(2003(3)2015) title("Predicted")

	tw line inclusionary_onsite2 inclusionary_onsite1 inclusionary_onsite0  yearpermit, ///
		lcolor(black gs8 gs4) lpattern(solid solid dash) lwidth(medthick medthick medthick)  ///
		graphregion(color(white)) legend(order(1 "GEA Expansion" 2 "Always GEA" 3 "Never GEA") rows(1) size(small)) ///
		ytitle("") name(gea_sh_actual, replace) xtitle("") ylabel(,nogrid) ///
		xscale(range(2003 2014)) xlabel(2003(3)2015) title("Actual")
	
	grc1leg gea_sh_actual gea_sh_pred, ycommon rows(1) ///
			name(actual_vs_pred_gea, replace) graphregion(color(white)) ///
			l1title("Share with Onsite Inclusionary Units", size(small)) ring(1)			
	
	restore
		
*** NPP areas

	preserve
	
	bys ct2010: egen sh_npp = mean(npp)
	keep if sh_npp > 0
	
	replace npp = 0 if missing(npp)

	collapse (mean) pr_421a inclusionary_onsite dtaxrate_onsite, by(yearpermit npp)
	
	reshape wide pr_421a inclusionary_onsite dtaxrate_onsite, i(year) j(npp)
	
	tw line dtaxrate_onsite1 dtaxrate_onsite0 yearpermit, ///
		lcolor(black gs8) lpattern(solid dash) lwidth(medthick medthick)  ///
		graphregion(color(white)) legend(order(1 "NPP Area" 2 "Non-NPP Area, Overlapping Tracts") size(small)) ///
		ytitle("Average 421-a Incentive") name(npp_rate, replace) xtitle("") ylabel(,nogrid) ///
		xscale(range(2003 2014)) xlabel(2003(3)2015)
		
	tw line inclusionary_onsite1 inclusionary_onsite0 yearpermit, ///
		lcolor(black gs8) lpattern(solid dash) lwidth(medthick medthick) ///
		graphregion(color(white)) legend(order(1 "NPP Area" 2 "Non-NPP Area, Overlapping Tracts") size(small)) ///
		ytitle("Share with Onsite Inclusionary Units") name(npp_sh, replace) xtitle("") ylabel(,nogrid) ///
		xscale(range(2003 2014)) xlabel(2003(3)2015)
		
	grc1leg npp_rate npp_sh, graphregion(color(white)) name(gr2, replace)
	
	* Check goodness-of-fit
	
	tw line pr_421a1 pr_421a0  yearpermit, ///
		lcolor(black gs8) lpattern(solid dash) lwidth(medthick medthick) ///
		graphregion(color(white)) legend(order(1 "NPP Area" 2 "Non-NPP Area, Overlapping Tracts") size(small)) ///
		ytitle("") name(npp_sh_pred, replace) xtitle("") ylabel(,nogrid) ///
		xscale(range(2003 2014)) xlabel(2003(3)2015) title("Predicted")

	tw line inclusionary_onsite1 inclusionary_onsite0  yearpermit, ///
		lcolor(black gs8) lpattern(solid dash) lwidth(medthick medthick) ///
		graphregion(color(white)) legend(order(1 "NPP Area" 2 "Non-NPP Area, Overlapping Tracts") size(small)) ///
		ytitle("") name(npp_sh_actual, replace) xtitle("") ylabel(,nogrid) ///
		xscale(range(2003 2014)) xlabel(2003(3)2015) title("Actual")
	
	grc1leg npp_sh_actual npp_sh_pred, ycommon rows(1) ///
			name(actual_vs_pred_npp, replace) graphregion(color(white)) ///
			l1title("Share with Onsite Inclusionary Units", size(small)) ring(1)			
	
	restore

*** Stack graphs
	
	graph combine gr1 gr2, rows(2) ysize(5) xsize(6.5) graphregion(color(white))
	
	graph export "$figs/gea_npp_visualization.pdf", replace
	graph export "$figs_overleaf/gea_npp_visualization.pdf", replace
	
	graph combine actual_vs_pred_gea actual_vs_pred_npp, rows(2) ysize(5) xsize(6.5) graphregion(color(white))
	
	graph export "$figs/gea_npp_goodness_of_fit.pdf", replace
	graph export "$figs_overleaf/gea_npp_goodness_of_fit.pdf", replace
	
